package com.web.activiti.shiro;

import com.web.activiti.pojo.ActiveUser;
import com.web.activiti.pojo.Employee;
import com.web.activiti.pojo.SysPermission;
import com.web.activiti.pojo.TreeMenu;
import com.web.activiti.service.EmployeeService;
import com.web.activiti.service.Impl.SysServiceImpl;
import com.web.activiti.service.SysService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.realm.RealmFactory;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
 * shiro认证及授权
 */
public class CustomRealm extends AuthorizingRealm {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private SysService sysService;


    /**
     * 执行授权
     * @param principal
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo (PrincipalCollection principal) {
        ActiveUser activeUser = (ActiveUser) principal.getPrimaryPrincipal();
        //查询数据库认证用户拥有的角色和权限
        List<SysPermission> permissions = null;
        try {
            permissions = sysService.findPermissionListByUserId(activeUser.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<String> permisionList = new ArrayList<>();
        for (SysPermission sysPermission : permissions) {
            permisionList.add(sysPermission.getPercode());
        }

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.addStringPermissions(permisionList);

        return info;
    }

    /**
     * 认证
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo (AuthenticationToken token) throws AuthenticationException {
        System.out.println("用户登录。。。");
        String username= (String) token.getPrincipal();
        System.out.println(username);
        Employee user=null;
        try {
            user = employeeService.findUserByUsername(username);
//            System.out.println("id="+user.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (user==null){
            return null;
        }
        List<TreeMenu> list= null;
        try {
            list = sysService.loadTreeMenu();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(Arrays.toString(list.toArray()));
        ActiveUser activeUser = new ActiveUser();
        activeUser.setId(user.getId());
        activeUser.setUserid(user.getName());
        activeUser.setUsercode(user.getName());
        activeUser.setUsername(user.getName());
        activeUser.setManagerId(user.getManagerId());
        activeUser.setMenuTree(list);
        String password_db=user.getPassword();
        String salt =user.getSalt();
        SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(activeUser,password_db, ByteSource.Util.bytes(salt),
                "CustomRealm");
        return info;

    }
}
